var canQuest = false;
var app = new Vue({
    el: '#app',
    data: {
        pcList: {},
        pcContents: '',
        pageFalg: true,
        tabs: {},
        tabsidx: 1,
        end_time_down: "",
        coutdownTimeStr: '00：00：00',
        recordData: [],
        list: [],
        hideing: true,
        equList: [],
        equclassify: [],
        pageIndex: 1,
        type: 1,
        pageSize: 12,
        total: 0,
        loading: true,
        noMore: false,
        categoryId: 0,
        canloading: false,
        userId: 0,
    },
    created: function() {
        FastClick.attach(document.body);
        $(document.body).infinite();
        if ($.getPara("userId") != "") {
            var userId = $.getPara("userId");
            this.userId = userId;
            $.setStorage("userId", userId)
        } else {
            if ($.getStorage("userId") && $.getStorage("userId") != "") {
                var userId = parseInt($.getStorage("userId"));
                this.userId = userId
            }
        }
        var that = this;
        that.getPcList();
        that.getList();
        FastClick.attach(document.body);
        that.getEquClassify();
        that.getEquList(1)
    },
    mounted() {
        var that = this;
        this.$nextTick(function() {
            $(document.body).infinite().on("infinite", function() {
                if (canQuest)
                    return;
                canQuest = true;
                that.hideing = false;
                setTimeout(function() {
                    var pageIndex = that.pageIndex;
                    pageIndex++;
                    that.pageIndex = pageIndex;
                    that.getEquList(pageIndex);
                    canQuest = false
                }, 1500)
            });
           
        })
    },
    methods: {
        getPcList() {
            var that = this;
            $.ajax({
                type: "GET",
                url:  "/api/device/evaluate/list",
                dataType: 'json',
                data: {
                    timeType: 0,
                    pageIndex: 1,
                    pageSize: 5
                },
                success: function(res) {
                    // console.log(res);
                    if (res.code == 200) {
                        if (res.data != "") {
                            that.pcList = res.data;
                            var list = that.pcList.list;
                            list.forEach(function(val, index) {
                                that.pcContents += '<div class="swiper-slide" onclick="pcDetails(' + val.id + ')"><img src="' + val.head_img + '" /><div class="title">' + val.title + '</div></div>'
                            });
                            // console.log(that.pcContents);
                            setTimeout(function() {
                                var swiper = new Swiper('.swiper1',{
                                    autoplay: 3000,
                                    loop: true,
                                    pagination: '.swiper-pagination1',
                                    autoplayDisableOnInteraction: true,
                                })
                            }, 1)
                        }
                    }
                },
            })
        },
        getList() {
            var that = this;
            $.ajax({
                url:  "/api/seckill/index/list",
                type: 'get',
                dataType: 'json',
                data: {},
            }).done(function(res, status, xhr) {
                if (res.code == 200) {
                    var data = res.data;
                    setTimeout(function() {
                        var swiper2 = new Swiper('.swiper2', {
                            autoplay:false,
                            loop: false,
                            slidesPerView: 3,
                            spaceBetween: 30,
                            // pagination: {
                            //   el: '.swiper-pagination',
                            //   clickable: true,
                            // },
                        });
                    }, 1)
                    that.getTabs(data)
                } else {
                    $.toast(res.msg, "text")
                }
            })
        },
        getTabs(data) {
            var that = this;
            if (data) {
                var obj = {};
                obj.id = data.id;
                obj.end_time = data.end_time;
                obj.end_timeStr = data.end_time.toString().substr(11, 5);
                obj.start_time = data.start_time;
                obj.start_timeStr = data.start_time.toString().substr(11, 5);
                var startTime = new Date(GetDateDiff(data.start_time)).getTime();
                var endTime = new Date(GetDateDiff(data.end_time)).getTime();
                var currentDate = new Date().getTime();
                if (currentDate >= startTime && currentDate <= endTime) {
                    obj.textFlag = true;
                    obj.text = "秒杀中";
                    that.tabsidx = data.id;
                    that.end_time_down = data.end_time
                } else {
                    obj.textFlag = false;
                    obj.text = "即将开始"
                }
                this.tabs = obj;
                if (that.end_time_down != "") {
                    this.grouponcountdown()
                } else {}
                that.recordData = data;
                that.showList()
            } else {
                this.pageFalg = false
            }
        },
        grouponcountdown() {
            var that = this;
            var end_time = GetDateDiff(that.end_time_down);
            var EndTime = new Date(end_time).getTime();
            var NowTime = new Date().getTime();
            var total_micro_second = EndTime - NowTime;
            if (total_micro_second <= 0) {
                that.coutdownTimeStr = "已结束"
            } else {
                that.coutdownTimeStr = dateformat(total_micro_second)
            }
            ;setTimeout(function() {
                that.grouponcountdown(that, end_time)
            }, 1000)
        },
        showList() {
            var that = this;
            var id = that.tabsidx;
            var data = that.recordData;
            var array = [];
            $.each(data.goodsList, function(k, item) {
                var obj = {};
                obj.goods_id = item.goods_id;
                obj.goods_name = item.goods_name;
                obj.head_img = item.head_img;
                obj.original_price = item.original_price;
                obj.price = item.price;
                obj.selled_stock = item.selled_stock;
                obj.spec_id = item.spec_id;
                obj.total_stock = item.total_stock;
                if (item.total_stock - item.selled_stock <= 0) {
                    obj.hideing = true
                } else {
                    obj.hideing = false
                }
                obj.percentage = (item.selled_stock / item.total_stock * 100).toFixed(2);
                array.push(obj)
            });
            that.list = array
        },
        getEquList(pageIndex) {
            var that = this;
            that.loading = true;
            var pageIndex = pageIndex;
            var categoryId = that.categoryId;
            var pageSize = that.pageSize;
            $.ajax({
                type: "GET",
                url:  "/api/device/list",
                dataType: 'json',
                data: {
                    categoryId: categoryId,
                    pageIndex: pageIndex,
                    pageSize: pageSize
                },
                success: function(res) {
                    if (res.code == 200) {
                        var list = res.data.list;
                        if (list.length > 0) {
                            $.each(list, function(index, val) {
                                that.equList.push(val)
                            });
                            that.noMore = false
                        } else {
                            canQuest = true;
                            that.noMore = true
                        }
                        that.hideing = true;
                        that.loading = false
                    } else {
                        $.toast(res.msg, "text")
                    }
                }
            })
        },
        getEquClassify() {
            var that = this;
            $.ajax({
                type: "GET",
                url:  "/api/device/category/list",
                dataType: 'json',
                data: {},
                success: function(res) {
                    if (res.code == 200) {
                        if (res.data) {
                            that.equclassify = res.data
                        }
                    }
                }
            })
        },
        changeId(id) {
            var that = this;
            canQuest = false;
            that.categoryId = id;
            that.equList = [];
            that.pageIndex = 1;
            that.getEquList(1)
        },
        gotoPcList(item) {
            var para = {};
            para.title = "评测列表";
            para.type = "equEvaluating";
            para.url = '/template/equEvaluatingList.html';
            if (this.userId > 0) {
                para.url = '/template/equEvaluatingList.html?userId=' + this.userId
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("请使用移动设备！")
            }
        },
        pcDetails(item) {
            var para = {};
            para.title = "评测详情";
            para.type = "equEvaluating";
            para.url = '/template/equEvaluatingDetails.html?id=' + item.id;
            if (this.userId > 0) {
                para.url = '/template/equEvaluatingDetails.html?id=' + item.id + "&userId=" + this.userId
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("请使用移动设备！")
            }
        },
        toMsList(item) {
            var para = {};
            para.title = "秒杀列表";
            para.type = "shop";
            para.url = '/PageClassify/html/Seckill/secKill.html';
            if (this.userId > 0) {
                para.url = '/PageClassify/html/Seckill/secKill.html?userId=' + this.userId
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("请使用移动设备！")
            }
        },
        toMsDetails(item) {
            var para = {};
            para.title = "秒杀详情";
            para.type = "shop";
            para.url = '/template/CommodityDetails.html?id=' + item.goods_id;
            if (this.userId > 0) {
                para.url = '/template/CommodityDetails.html?id=' + item.goods_id + "&userId=" + this.userId
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("请使用移动设备！")
            }
        },
        goequdetails(item) {
            var para = {};
            para.title = "设备详情";
            para.type = "equEvaluating";
            para.url = '/template/equipmentDetails.html?id=' + item.id;
            if (this.userId > 0) {
                para.url = '/template/equipmentDetails.html?id=' + item.id + "&userId=" + this.userId
            }
            if (/(Android)/i.test(navigator.userAgent)) {
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                window.webkit.messageHandlers.appPush.postMessage(para)
            } else {
                window.location.href = para.url;
                alert("请使用移动设备！")
            }
        },
    }
});
function dateformat(micro_second) {
    var second = Math.floor(micro_second / 1000);
    var day = Math.floor(second / 3600 / 24);
    var hr = Math.floor(second / 3600 % 24);
    var hrStr = hr.toString();
    if (hrStr.length == 1)
        hrStr = '0' + hrStr;
    var min = Math.floor(second / 60 % 60);
    var minStr = min.toString();
    if (minStr.length == 1)
        minStr = '0' + minStr;
    var sec = Math.floor(second % 60);
    var secStr = sec.toString();
    if (secStr.length == 1)
        secStr = '0' + secStr;
    return "<div>" + hrStr + "</div>:<div>" + minStr + "</div>:<div>" + secStr + "</div>"
}
function GetDateDiff(DiffTime) {
    Time = DiffTime.replace(/\-/g, "/");
    return Time
}
